package com.cyz.common.utils;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.cyz.common.enums.MyEnums;
import lombok.Data;

/**
 * 响应数据统一工具
 */
@Data
//该注解可以再返回数据时，把未赋值的属性忽略掉
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R<T> {

    //返回码
    private Integer code;
    //返回信息
    private String msg;
    //返回数据
    private T data;

    public R(T data){
        this.code = MyEnums.SUCCESS.getCode();
        this.msg = MyEnums.SUCCESS.getMsg();
        this.data = data;
    }

    public R(Integer code,String msg){
        this.code = code;
        this.msg = msg;
    }

    public R(MyEnums myEnum){
        this.code = myEnum.getCode();
        this.msg = myEnum.getMsg();
    }

//    public R(MyEnums myEnum,T data){
//        this.code = myEnum.getCode();
//        this.msg = myEnum.getMsg();
//        this.data = data;
//    }

    public static R success(){
        return new R(MyEnums.SUCCESS);
    }

    public static R success(MyEnums myEnum){
        return new R(myEnum);
    }

    public static R success(Object data){
        return new R(data);
    }

    public static R error(MyEnums myEnum){
        return new R(myEnum);
    }

    public static R error(Integer code,String msg){
        return new R(code,msg);
    }

    public static R error(MyEnums myEnum,String msg){
        return new R(myEnum.getCode(),msg);
    }
}
